package message

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/message"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type NoticeMessageInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 公告消息详情
func NewNoticeMessageInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NoticeMessageInfoLogic {
	return &NoticeMessageInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *NoticeMessageInfoLogic) NoticeMessageInfo(req *types.NoticeMessageInfoReq) (resp *types.NoticeMessageDataItemRes, err error) {
	if req.Id == 0 {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.Message.NoticeMessageInfo(l.ctx, &message.NoticeMessageInfoReq{Id: req.Id, MerchantId: 1})
	if err != nil {
		logc.Error(l.ctx, "NoticeMessageInfo err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	if u.Data.Id == 0 {
		return nil, code.NotData
	}

	return &types.NoticeMessageDataItemRes{
		Id:         u.Data.Id,
		MerchantId: u.Data.MerchantId,
		Type:       u.Data.Type,
		Title:      u.Data.Title,
		Content:    u.Data.Content,
		Status:     u.Data.Status,
		UpdatedAt:  u.Data.UpdatedAt,
	}, nil
}
